package com.ruoyi.media.accountGroup.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.media.accountGroup.domain.MediaAccountGroup;
import com.ruoyi.media.accountGroup.domain.bo.MediaAccountGroupBo;
import com.ruoyi.media.accountGroup.domain.vo.MediaAccountGroupVo;
import com.ruoyi.media.accountGroup.mapper.MediaAccountGroupMapper;
import com.ruoyi.media.accountGroup.service.IMediaAccountGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 账号分组Service业务层处理
 *
 * @author dqw
 * @date 2023-05-26
 */
@RequiredArgsConstructor
@Service
public class MediaAccountGroupServiceImpl extends ServiceImpl<MediaAccountGroupMapper,MediaAccountGroup> implements IMediaAccountGroupService {

    private final MediaAccountGroupMapper baseMapper;

    /**
     * 查询账号分组
     */
    @Override
    public MediaAccountGroupVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询账号分组列表
     */
    @Override
    public TableDataInfo<MediaAccountGroupVo> queryPageList(MediaAccountGroupBo bo, PageQuery pageQuery) {
        //获取登录用户 只能获取当前登录账号的数据
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setUserId(loginUser.getUserId());
        LambdaQueryWrapper<MediaAccountGroup> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(MediaAccountGroup::getCreateTime);
        Page<MediaAccountGroupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }



    /**
     * 查询账号分组列表
     */
    @Override
    public List<MediaAccountGroupVo> queryList(MediaAccountGroupBo bo) {
        LambdaQueryWrapper<MediaAccountGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MediaAccountGroup> buildQueryWrapper(MediaAccountGroupBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MediaAccountGroup> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, MediaAccountGroup::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, MediaAccountGroup::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountId()), MediaAccountGroup::getAccountId, bo.getAccountId());
        lqw.like(StringUtils.isNotBlank(bo.getGroupTitle()), MediaAccountGroup::getGroupTitle, bo.getGroupTitle());
        return lqw;
    }

    /**
     * 新增账号分组
     */
    @Override
    public Boolean insertByBo(MediaAccountGroupBo bo) {
        MediaAccountGroup add = BeanUtil.toBean(bo, MediaAccountGroup.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改账号分组
     */
    @Override
    public Boolean updateByBo(MediaAccountGroupBo bo) {
        MediaAccountGroup update = BeanUtil.toBean(bo, MediaAccountGroup.class);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MediaAccountGroup entity){
        //TODO 做一些数据校验,如唯一约束
        LoginUser loginUser =  LoginHelper.getLoginUser();
        List<MediaAccountGroup> mediaAccountGroupList =  baseMapper.getAccountDataByTitle(loginUser.getUserId(),entity.getGroupTitle());
        if(mediaAccountGroupList.size() > 0){
            throw new RuntimeException("已存在该分组！");
        }
        entity.setUserId(loginUser.getUserId());
        entity.setDeptId(loginUser.getDeptId());
    }

    /**
     * 批量删除账号分组
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


}
